package com.saikuo.service.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by chenzhuo on 16/7/28.
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String YMDHMS_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String YMDHM_FROMAT = "yyyy-MM-dd HH:mm";

    public static int daysOfTwoDate(Date start, Date end) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        int startDay = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(end);
        int endDay = calendar.get(Calendar.DAY_OF_YEAR);
        return endDay - startDay;
    }

    public static String getOffsetDate(Date start, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        calendar.add(Calendar.DAY_OF_MONTH, offset);
        return DateFormatUtils.format(calendar, YYYY_MM_DD);
    }

    public static String formatYMD(Date date) {
        return DateFormatUtils.format(date, YYYY_MM_DD);
    }

    public static String getCurrTime() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return outFormat.format(now);
    }


    public static String getDateOfYMDHMSString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(YMDHMS_FORMAT);
        return format.format(date);
    }

    public static String getDateOfYMDHMString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(YMDHM_FROMAT);
        return format.format(date);
    }
}
